set ansi_padding, ansi_warnings, concat_null_yields_null, arithabort, quoted_identifier, ansi_nulls on
go

if exists (select * from tempdb..sysobjects where id=object_id('tempdb..#tmpProperties')) drop table #tmpProperties
go

create table #tmpProperties ( name nvarchar(255), value nvarchar(255) )
go

insert into #tmpProperties values('script', 'update.0014.sql')
insert into #tmpProperties values('error', '0')

set xact_abort on
go

set transaction isolation level serializable
go

begin transaction
go

set ansi_nulls on
set quoted_identifier on
go
/* BEGIN UPDATE SCRIPT */
--============== Ring-back ton
delete MODULES where id='EBB37ECD-19EB-4312-B4DF-08B9F911EBE1'
go
INSERT INTO [dbo].[MODULES]([ID], [DELETED], [CREATED_BY], [DATE_ENTERED], [MODIFIED_USER_ID], [DATE_MODIFIED], [DATE_MODIFIED_UTC], [MODULE_NAME], [DISPLAY_NAME], [RELATIVE_PATH], [MODULE_ENABLED], [TAB_ENABLED], [MOBILE_ENABLED], [TAB_ORDER], [PORTAL_ENABLED], [CUSTOM_ENABLED], [REPORT_ENABLED], [IMPORT_ENABLED], [SYNC_ENABLED], [IS_ADMIN], [CUSTOM_PAGING], [MASS_UPDATE_ENABLED], [DEFAULT_SEARCH_ENABLED], [TABLE_NAME], [EXCHANGE_SYNC], [EXCHANGE_FOLDERS], [EXCHANGE_CREATE_PARENT])
SELECT N'EBB37ECD-19EB-4312-B4DF-08B9F911EBE1', 0, NULL, getdate(), N'00000000-0000-0000-0000-000000000001', getdate(), getdate(), N'RingBackTone', N'.moduleList.RingbackTone', N'~/RingBackTone/', 1, 1, 1, 9, 0, 0, 1, 0, 0, 0, 0, 1, 1, 'TERMONOLOGY', 0, 0, 0
go
delete TERMINOLOGY where id ='DFA522B6-3B3D-474C-A2FC-29883A1B0D2D'
go
INSERT INTO [dbo].[TERMINOLOGY] 
(ID,[NAME],[LIST_NAME],[DISPLAY_NAME],[LANG],[DELETED],[DATE_ENTERED],[DATE_MODIFIED],[DATE_MODIFIED_UTC])
VALUES
	   ('DFA522B6-3B3D-474C-A2FC-29883A1B0D2D'
	   ,'RingBackTone'
	   ,'moduleList'
	   ,'Ring-back Tone'
	   ,'en-US',0,getdate(),getdate(),getdate())
go
delete CONFIG where id='C5EF8D4E-9372-44D5-8B49-02BBEF7BD29B'
go
INSERT INTO [dbo].[CONFIG]
           ([ID]
           ,[DELETED]
           ,[CREATED_BY]
           ,[DATE_ENTERED]
           ,[MODIFIED_USER_ID]
           ,[DATE_MODIFIED]
           ,[DATE_MODIFIED_UTC]
           ,[CATEGORY]
           ,[NAME]
           ,[VALUE])
     VALUES
           ('C5EF8D4E-9372-44D5-8B49-02BBEF7BD29B'
           ,0
           ,null
           ,getdate()
           ,null
           ,getdate()
           ,getdate()
           ,'CallCenter'
           ,'RingBackToneURL'
           ,'')
GO 


--============== OTA

delete MODULES where id='2D223B46-A37C-4092-B97D-24A3C97FCA57'
go
INSERT INTO [dbo].[MODULES]([ID], [DELETED], [CREATED_BY], [DATE_ENTERED], [MODIFIED_USER_ID], [DATE_MODIFIED], [DATE_MODIFIED_UTC], [MODULE_NAME], [DISPLAY_NAME], [RELATIVE_PATH], [MODULE_ENABLED], [TAB_ENABLED], [MOBILE_ENABLED], [TAB_ORDER], [PORTAL_ENABLED], [CUSTOM_ENABLED], [REPORT_ENABLED], [IMPORT_ENABLED], [SYNC_ENABLED], [IS_ADMIN], [CUSTOM_PAGING], [MASS_UPDATE_ENABLED], [DEFAULT_SEARCH_ENABLED], [TABLE_NAME], [EXCHANGE_SYNC], [EXCHANGE_FOLDERS], [EXCHANGE_CREATE_PARENT])
SELECT N'2D223B46-A37C-4092-B97D-24A3C97FCA57', 0, NULL, getdate(), N'00000000-0000-0000-0000-000000000001', getdate(), getdate(), N'OTAService', N'.moduleList.OTAService', N'~/OTAService/', 1, 1, 1, 9, 0, 0, 1, 0, 0, 0, 0, 1, 1, 'TERMONOLOGY', 0, 0, 0
go
delete TERMINOLOGY where id ='2BEAAA4C-4951-4EF4-AC6D-89ED6F518B38'
go
INSERT INTO [dbo].[TERMINOLOGY] 
(ID,[NAME],[LIST_NAME],[DISPLAY_NAME],[LANG],[DELETED],[DATE_ENTERED],[DATE_MODIFIED],[DATE_MODIFIED_UTC])
VALUES
	   ('2BEAAA4C-4951-4EF4-AC6D-89ED6F518B38'
	   ,'OTAService'
	   ,'moduleList'
	   ,'OTA Service'
	   ,'en-US',0,getdate(),getdate(),getdate())
go
delete CONFIG where id='06ABD1F5-8911-4C90-AE87-F0FD8191F3A1'
go
INSERT INTO [dbo].[CONFIG]
           ([ID]
           ,[DELETED]
           ,[CREATED_BY]
           ,[DATE_ENTERED]
           ,[MODIFIED_USER_ID]
           ,[DATE_MODIFIED]
           ,[DATE_MODIFIED_UTC]
           ,[CATEGORY]
           ,[NAME]
           ,[VALUE])
     VALUES
           ('06ABD1F5-8911-4C90-AE87-F0FD8191F3A1'
           ,0
           ,null
           ,getdate()
           ,null
           ,getdate()
           ,getdate()
           ,'CallCenter'
           ,'OTA_URL'
           ,'http://www.ngoisao.net')
GO 
/* END UPDATE SCRIPT */
go
if @@error<>0 and @@trancount>0 rollback transaction
go

if @@trancount=0 begin
	update #tmpProperties set value = 1 where name = 'error'
	begin transaction
end
go

if (select top 1 value from #tmpProperties where name = 'error') <> '0' rollback transaction
go

if @@trancount > 0 begin
	declare @@scriptName nvarchar(255)
	select top 1 @@scriptName = value from #tmpProperties where name = 'script'

	if dbo.scriptExisted(@@scriptName) = 1 begin
		rollback transaction
		print @@scriptName + ' already executes'
	end
	else begin
		exec dbo.updateScript @@scriptName
		commit transaction
		print 'The database update succeeded'
	end
END
else begin
	print 'The database update failed'
end
go

drop table #tmpProperties
go
